#!/bin/sh
# Copyright (C) 2020 Ycarus (Yannick Chabanois) <ycarus@zugaina.org> for OpenMPTCProuter project

if [ "$(uci -q get shadowsocks-libev.hi)" != ""; then
	uci -q batch <<-EOF >/dev/null
		rename shadowsocks-libev.hi=hi1
		commit shadowsocks-libev
	EOF
fi

if [ "$(uci -q get openmptcprouter.latest_versions)" = "" ]; then
	NBCPU=$(grep -c '^processor' /proc/cpuinfo | tr -d "\n")
	if [ "$NBCPU" -gt 2 ]; then
		if [ "$(uci -q get shadowsocks-libev.hi3)" = "" ]; then
			for c in $(seq 3 $NBCPU); do
				uci -q batch <<-EOF >/dev/null
					set shadowsocks-libev.hi$c=ss_redir
					set shadowsocks-libev.hi$c.server=sss0
					set shadowsocks-libev.hi$c.local_address="0.0.0.0"
					set shadowsocks-libev.hi$c.local_port=1101
					set shadowsocks-libev.hi$c.mode=tcp_and_udp
					set shadowsocks-libev.hi$c.timeout=1000
					set shadowsocks-libev.hi$c.fast_open=1
					set shadowsocks-libev.hi$c.reuse_port=1
					set shadowsocks-libev.hi$c.syslog=1
					set shadowsocks-libev.hi$c.mptcp=1
					set shadowsocks-libev.hi$c.no_delay=1
					set shadowsocks-libev.hi$c.verbose=0
				EOF
			done
			uci -q set shadowsocks-libev.ss_rules.redir_tcp=all
			uci -q commit shadowsocks-libev
		fi
	fi

	if [ "$(uci -q get shadowsocks-libev.hi1.timeout)" != "1000" ]; then
		for c in $(seq 1 $NBCPU); do
			uci -q batch <<-EOF > /dev/null
				set shadowsocks-libev.hi$c.timeout=1000
				commit shadowsocks-libev
			EOF
		done
	fi
	if [ "$(uci -q get shadowsocks-libev.hi1.mode)" != "tcp" ]; then
		for c in $(seq 1 $NBCPU); do
			uci -q batch <<-EOF > /dev/null
				set shadowsocks-libev.hi$c.mode=tcp_and_udp
				commit shadowsocks-libev
			EOF
		done
	fi
fi

if [ "$(uci -q get shadowsocks-libev.hi3)" != "" ]; then
	port=1100
	[ "$NBCPU" -gt "10" ] && NBCPU=10
	for c in $(seq 1 2 $NBCPU); do
		uci -q batch <<-EOF >/dev/null
			set shadowsocks-libev.hi$c.local_port=$port
			set shadowsocks-libev.hi$c.server=sss0
			set shadowsocks-libev.hi$((c+1))=ss_redir
			set shadowsocks-libev.hi$((c+1)).local_address="::"
			set shadowsocks-libev.hi$((c+1)).local_port=$port
			set shadowsocks-libev.hi$((c+1)).local_address="::"
			set shadowsocks-libev.hi$((c+1)).server=sss1
			set shadowsocks-libev.hi$((c+1)).mode=tcp_and_udp
			set shadowsocks-libev.hi$((c+1)).timeout=1000
			set shadowsocks-libev.hi$((c+1)).fast_open=1
			set shadowsocks-libev.hi$((c+1)).reuse_port=1
			set shadowsocks-libev.hi$((c+1)).syslog=1
			set shadowsocks-libev.hi$((c+1)).mptcp=1
			set shadowsocks-libev.hi$((c+1)).no_delay=1
			set shadowsocks-libev.hi$((c+1)).verbose=0
			commit shadowsocks-libev
		EOF
		port=$((port+1))
	done
	uci -q batch <<-EOF > /dev/null
		set shadowsocks-libev.ss_rules.redir_tcp=all
		commit shadowsocks-libev
	EOF
fi

if [ "$(uci -q get shadowsocks-libev.hi1.obfs)" = "1" ]; then
	uci -q batch <<-EOF > /dev/null
		set shadowsocks-libev.sss0.obfs=1
		set shadowsocks-libev.sss0.obfs_plugin="$(uci -q get shadowsocks-libev.hi1.obfs_plugin)"
		set shadowsocks-libev.sss0.obfs_host="$(uci -q get shadowsocks-libev.hi1.obfs_host)"
		set shadowsocks-libev.sss0.obfs_type="$(uci -q get shadowsocks-libev.hi1.obfs_type)"
		commit shadowsocks-libev
	EOF
fi

if [ "$(uci -q get shadowsocks-libev.ss_rules.server)" = "" ]; then
	uci -q batch <<-EOF > /dev/null
		set shadowsocks-libev.ss_rules.server="sss0"
		commit shadowsocks-libev
	EOF
fi


if [ "$(uci -q get ucitrack.@shadowsocks-libev[-1].affects | grep openmptcprouter-vps)" = "" ]; then
	uci -q batch <<-EOF >/dev/null
		add_list ucitrack.@shadowsocks-libev[-1].affects="openmptcprouter-vps"
		commit ucitrack
	EOF
fi

if [ "$(uci -q get shadowsocks-libev.sss1)" = "" ]; then
	uci -q batch <<-EOF > /dev/null
		set shadowsocks-libev.sss1=server
		set shadowsocks-libev.sss1.server_port='65101'
		set shadowsocks-libev.sss1.method='chacha20-ietf-poly1305'
		set shadowsocks-libev.sss1.obfs='0'
		set shadowsocks-libev.sss1.disabled='1'
		commit shadowsocks-libev
	EOF
fi

rm -f /tmp/luci-indexcache
exit 0
